package cn.xiaohaoo.datacollection;

import cn.hutool.http.HttpUtil;
import cn.xiaohaoo.medicine.util.mongo.MongoDatabaseUtils;

import java.io.File;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * 爬取图片
 */
public class MedicineImgPageProcessor3 {

    public static void main(String[] args) {
        LocalDateTime begin = LocalDateTime.now();
        AtomicInteger sum = new AtomicInteger();
        MongoDatabaseUtils.queryAllDocuments(MongoDatabaseUtils.MEDICINE_DETAIL_COLLECTION).forEach(v -> {
            Object img = v.get("img");
            if (img != null) {
                try {
                    HttpUtil.downloadFile(img.toString(), new File("D:\\data\\" + v.get("name").toString() + ".jpg"));
                    sum.getAndIncrement();
                } catch (Exception e) {
                    System.err.println(v.get("name") + "出错啦!");
                }
            }
        });
        System.out.println("总共下载次数：" + sum.get());
        System.out.println("爬取用时：" + Duration.between(begin, LocalDateTime.now()).toMinutes() + "分钟");
    }
}
